// mypage/accCash/accCash.js
var app=getApp();
var my=require("../../api/my");
var util=require("../../utils/util");
var config=require("../../api/config");

Page({
  /*页面的初始数据*/
  data: {
    text1: '',
    text2: '',
    text3: '',
    total_money: "0.00",
    cash_money: '',
    money1: '',
    money2: '',
    mech_id: '',
    mech_index: '',
    pages: ["../checkCard/checkCard?type=1","../checkMech/checkMech","../cashOrder/cashOrder"],
    pop_show: true,
    length: 6,
    isFocus: false,
    Value: "",
    token: '',
    update: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    that.setData({text2:options.name,mech_id:options.id,mech_index:options.index,total_money:options.money,token:wx.getStorageSync('token')})
  },
  // 跳转
  nav_go:function(e){
    var that=this,pages=that.data.pages;
    var index=e.currentTarget.dataset.index;
    wx.navigateTo({
      url: pages[index]+'?index='+that.data.mech_index+'&id='+that.data.mech_id,
    })
  },
  // 全部
  all_in:function(){
    var that = this,total_money=that.data.total_money,id=that.data.mech_id;
    if(total_money>0){
      that.get_mech_all_order(id)
    }else{
      util.page_show_toast('余额不足',2000)
    }
  },
  // 提现
  confirm_cash:function(){
    var that=this,orderNo=that.data.text3,id=that.data.mech_id,name=that.data.text2,money=that.data.cash_money,token=that.data.token;
    if(!name){
      util.page_show_toast('请选择可提现机构',2000)
      return false;
    }
    if(!orderNo){
      util.page_show_toast('请选择可提现订单',2000)
      return false;
    }
    if(money<1){
      util.page_show_toast('单笔提现最低金额不能低于1元',3000)
      return false;
    }
    if(money>5000){
      util.page_show_toast('单笔提现最高金额不能超过5000元',3000)
      return false;
    }
    var params={
      "withdrawOrderNo": orderNo,
      "collectionType": 1,
      "businessId": id,
      "businessName": name,
    }
    that.create_token(params,token)
    // that.user_cash(params)
  },
  onShow:function(){
    var that=this,mech_id=that.data.mech_id;
    if(mech_id){
      that.get_mech_cach(mech_id)
    }
  },
  // 根据机构编号查询机构账户余额
  get_mech_cach:function(id){
    var that=this,update=that.data.update;
    my.get_mech_cach({"businessId":id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        var money1=info.withdrawableBalance>=1000?util.toThousands(util.toDecimal(info.withdrawableBalance)):util.toDecimal(info.withdrawableBalance);
        if(update){
          that.setData({text3:'',cash_money:'',update:false})
        }
        //
        that.setData({total_money:info.withdrawableBalance,money1:money1})
      }
    })
  },
  //
  create_token:function(params,token){
    var that=this;
    config.create_token(token).then(res=>{
      if(res.data.code==200){
        that.user_cash(params,token,res.data.data)
      }
    })
  },
  // 提现
  user_cash:function(params,token,x_token){
    var that=this;
    my.user_cash(params,token,x_token).then(res=>{
      if(res.data.code==200){
        wx.redirectTo({
          url: '../accSuccess/accSuccess?type=1&money='+that.data.cash_money,
        })
      }
    })
  },
  // 获取全部提现订单号
  get_mech_all_order:function(id){
    var that=this;
    my.get_mech_all_order({"businessId":id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        var money2=info.withdrawAmount>1000?util.toThousands(util.toDecimal(info.withdrawAmount)):util.toDecimal(info.withdrawAmount);
        that.setData({cash_money:info.withdrawAmount,money2:money2,text3:info.orderIds})
      }
    })
  },
  // 支付密码(暂时不用)
  // open_pop:function(){
  //   var that = this;
  //   that.setData({pop_show: !that.data.pop_show,isFocus: true})
  // },
  // close_pop:function(){
  //   this.setData({pop_show: !this.data.pop_show,isFocus: false})
  // },
  // ver_focus(e){
  //   var that = this;
  //   var inputValue = e.detail.value;
  //   console.log(inputValue)
  //   that.setData({ Value:inputValue})
  //   if(inputValue.length == 6){
  //     wx.navigateTo({
  //       url: '../accSuccess/accSuccess?type=1',
  //     })
  //     that.setData({pop_show: !that.data.pop_show,isFocus: false,Value:''})
  //   }
  // }, 
  // ver_tap(){
  //   var that = this;
  //   that.setData({ isFocus:true, })
  // },
  // find_password:function(){
  //   wx.navigateTo({
  //     url: '../accSetting/accSetting',
  //   })
  //   this.setData({pop_show: !this.data.pop_show,isFocus: false})
  // }
})